<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="测量日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="examinationDate">
              <j-date placeholder="请选择测量日期" v-model="model.examinationDate"  style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="设备" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="deviceCode">
              <a-select v-model="model.deviceCode" placeholder="请选择晨检设备" allowClear>
                <a-select-option v-for="(item, key) in deviceList" :key="key" :value="item.id">
                  {{ item.deviceName }}
                </a-select-option>
              </a-select>
              <!-- <j-dict-select-tag type="list" v-model="model.deviceCode" dictCode="sk_device,device_name,id,del_flag=0 and" placeholder="请选择设备" /> -->
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="班级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classId">
              <j-dict-select-tag type="list"  v-model="model.classId"  dictCode="sk_class,class_name,id,del_flag = 0 order by sort_no asc" placeholder="请选择班级" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="学生" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentId">
              <j-dict-select-tag type="list"  v-model="model.studentId" :dictCode="`sk_student,name,id,del_flag=0 and class_id=${model.classId}`" placeholder="请选择学生" />
            </a-form-model-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-model-item label="幼儿" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="studentId">
              <j-dict-select-tag type="list" v-model="model.studentId" dictCode="sk_student,name,id,del_flag=0" placeholder="请选择幼儿" />
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="体温" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="temperature">
              <a-input-number v-model="model.temperature" :min="0" :precision="1" :max="45" placeholder="请输入体温" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="有无伤痕" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="scar">
              <j-multi-select-tag type="checkbox" v-model="model.scar" :optionsWithDisabled="optionsWithDisabledScar"  @change="changeDisabledScar" dictCode="scar" placeholder="请选择有无伤痕" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="有无上感现象" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="urti">
              <j-multi-select-tag type="checkbox" v-model="model.urti" :optionsWithDisabled="optionsWithDisabledUrti"  @change="changeDisabledUrti"  dictCode="urti" placeholder="请选择有无上感现象" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="有无服药" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="takeMedicine">
              <j-dict-select-tag type="radio" v-model="model.takeMedicine" dictCode="yes_no" placeholder="请选择有无服药" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="有无流鼻血" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="noseBleeding">
              <j-dict-select-tag type="radio" v-model="model.noseBleeding" dictCode="yes_no" placeholder="请选择有无流鼻血" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="有无嘴唇干裂" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="chappedLip">
              <j-dict-select-tag type="radio" v-model="model.chappedLip" dictCode="yes_no" placeholder="请选择有无嘴唇干裂" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="有无自带异物" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="foreignBody">
              <j-dict-select-tag type="radio" v-model="model.foreignBody" dictCode="yes_no" placeholder="请选择有无自带异物" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="其他" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="other">
              <a-textarea v-model="model.other" :maxLength="255" :autosize="{ minRows: 4 }" placeholder="请输入其他" />
              <div style="text-align: right;margin:0;line-height: 20px;" v-if="!formDisabled">{{model.other?model.other.length :0}}/255</div>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="晨检牌" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="morningCheckCard">
              <j-dict-select-tag type="radio" v-model="model.morningCheckCard" dictCode="check_card_type" placeholder="请选择晨检牌" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="处理方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="processingMethodId">
              <j-multi-select-tag type="checkbox" v-model="model.processingMethodId" dictCode="processing_method" placeholder="请选择处理方式" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="说明" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="comment">
              <a-textarea v-model="model.comment" :maxLength="255" :autosize="{ minRows: 4 }" placeholder="请输入说明" />
              <div style="text-align: right;margin:0;line-height: 20px;" v-if="!formDisabled">{{model.comment?model.comment.length :0}}/255</div>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="照片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="photo">
              <j-image-upload isMultiple :number="imgMaxNumber"   v-model="model.photo" ></j-image-upload>
              <div v-if="!formDisabled">最多上传 <span style="color: #f5222d;">{{ imgMaxNumber }}</span> 张照片</div>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import {getDictItemsFromCache} from '@/api/api'
  export default {
    name: 'SkMorningCheckRecordForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
            scar:'0',
            urti: '0',
            takeMedicine:0,
            noseBleeding:0,
            chappedLip:0,
            foreignBody:0,
            morningCheckCard:0,
         },
        imgMaxNumber:5,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           examinationDate: [
              { required: true, message: '请选择测量日期!'},
           ],
          //  deviceCode: [
          //     { required: true, message: '请选择设备!'},
          //  ],
           classId: [
              { required: true, message: '请选择班级!'},
           ],
           studentId: [
              { required: true, message: '请选择学生!'},
           ],
           temperature: [
              { required: true, message: '请输入体温!'},
           ],
           morningCheckCard: [
              { required: true, message: '请输入晨检牌!'},
           ],
        },
        url: {
          add: "/health/skMorningCheckRecord/add",
          edit: "/health/skMorningCheckRecord/edit",
          queryById: "/health/skMorningCheckRecord/queryById"
        },
        optionsWithDisabledScar:[],
        optionsWithDisabledUrti:[],
        deviceList:[]
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
      
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.getDevice();
    },
    methods: {
      // 获取晨检设备
      getDevice(){
        const url = "/management/skDevice/listAll";
        const params = {
          deviceType: 7
        }
        getAction(url,params).then((res)=>{
          if(res.success){
            this.deviceList = res.result
          } else {
            this.message.warning(res.message);
          }
        }).catch((error)=>{
          console.error(error);
        })
      },
      changeDisabledScar(selectedValue){
        console.log('mutil select', selectedValue)
        let dicts = getDictItemsFromCache("scar");
        if(selectedValue == '0' || selectedValue == '') {
          if(selectedValue == '') {
            this.model.scar = '0'
          }
          this.optionsWithDisabledScar = dicts
        } else {
          //去掉'无'的勾选
          const selectDicts = selectedValue.split(',');
          const defaultIndex = selectDicts.indexOf('0');
          if(defaultIndex > -1){
            const newDicts = selectDicts.filter(item => {
              return item != '0'
            });
            console.log(newDicts.join(','))
            this.model.scar = newDicts.join(',')
          }
          //如果勾选了其他，'无'不能勾选
          this.optionsWithDisabledScar = dicts.map(item => ({
            value: item.value,
            label: item.label,
            disabled: item.value == '0' ? true : false
          }))
        } 
      },
      changeDisabledUrti(selectedValue){
        let dicts = getDictItemsFromCache("urti");
        if(selectedValue == '0' || selectedValue == '') {
          if(selectedValue == '') {
            this.model.urti = '0'
          }
          this.optionsWithDisabledUrti = dicts
        } else {
          //去掉'无'的勾选
          const selectDicts = selectedValue.split(',');
          const defaultIndex = selectDicts.indexOf('0');
          if(defaultIndex > -1){
            const newDicts = selectDicts.filter(item => {
              return item != '0'
            });
            this.model.urti = newDicts.join(',')
          }
          //如果勾选了其他，'无'不能勾选
          this.optionsWithDisabledUrti = dicts.map(item => ({
            value: item.value,
            label: item.label,
            disabled: item.value == '0' ? true : false
          }))
        } 
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
        this.changeDisabledScar(this.model.scar)
        this.changeDisabledUrti(this.model.urti)
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>
<style scoped lang="less">
/deep/ .ant-input-number{
  position: relative;
}
/deep/ .ant-input-number::before{
  content: "℃";
  position: absolute;
  top: 0;
  right: 32px;
  height: 32px;
  line-height: 32px;
  color: #999;
  font-size: 14px;
}
</style>